<?php

/**
 * myPropelUniqueValidator validates that the uniqueness of a column.
 * This validator only works for multiple columns with multiple primary keys.
 *
 * <b>Required parameters:</b>
 *
 * # <b>class</b>        - [none]               - Propel class name.
 * # <b>table_name</b>       - [none]               - database table name.
 * # <b>columns</b>       - [none]               - Propel column name/s.
 * # <b>primary_keys</b>       - [none]               - primary key column name/s.
 * # <b>form_columns</b>       - [none]               - form field name/s.
 * # <b>form_primary_keys</b>       - [none]               - form primary key name/s.
 *
 * <b>Optional parameters:</b>
 *
 * # <b>unique_error</b> - [Uniqueness error]   - An error message to use when
 *                                                the value for this column already
 *                                                exists in the database.
 *
 * @author     Milwell Sia <milwell@milwell.com>
  */
class myPropelUniqueValidator extends sfValidator
{
  public function execute(&$value, &$error)
  {
    $class  = $this->getParameter('class');  
    $classPeer  = $class.'Peer';

    $split = preg_split("/(?<=[a-z]) (?=[A-Z])/x", $class);
    $tablename = strtolower(implode('_',$split));
 
    //$tableName  = $this->getParameter('table_name');
    $columnNames = $this->getParameter('columns');
    $formColumnNames = $this->getParameter('form_columns');

    if((count($columnNames))!==(count($formColumnNames))){
      throw new sfValidatorException('The "columns" and "form_columns" parameter count not equal.');    
    }
    
    $primaryKeys = $this->getParameter('primary_keys');
    $formPrimaryKeys = $this->getParameter('form_primary_keys');

    if((count($primaryKeys))!==(count($formPrimaryKeys))){
      throw new sfValidatorException('The "primary_keys" and "form_primary_keys" parameter count not equal.');    
    }
    
    $c = new Criteria();
    
    for($i=0;$i<count($columnNames);$i++){
      $c->addAnd($tablename.'.'.$columnNames[$i], 
        $this->getContext()->getRequest()->getParameter(
          $formColumnNames[$i]
        )
      );
    }
    
    for($i=0;$i<count($primaryKeys);$i++){
      $c->addAnd($tablename.'.'.$primaryKeys[$i], 
        $this->getContext()->getRequest()->getParameter(
          $formPrimaryKeys[$i]
        ),
        Criteria::NOT_EQUAL
      );
    }
    
    
    $count = call_user_func(array($classPeer, 'doCount'), $c);
    
    if ($count>0)
    {
      $error = $this->getParameter('unique_error');
      return false;
    }
    return true;
  }

  /**
   * Initialize this validator.
   *
   * @param sfContext The current application context.
   * @param array   An associative array of initialization parameters.
   *
   * @return bool true, if initialization completes successfully, otherwise false.
   */
  public function initialize($context, $parameters = null)
  {
    // initialize parent
    parent::initialize($context);

    // set defaults
    $this->setParameter('unique_error', 'Uniqueness error');

    $this->getParameterHolder()->add($parameters);

    // check parameters
    if (!$this->getParameter('class'))
    {
      throw new sfValidatorException('The "class" parameter is mandatory for the sfPropelUniqueValidator validator.');
    }



    if (!$this->getParameter('primary_keys'))
    {
      throw new sfValidatorException('The "primary_keys" parameter is mandatory for the sfPropelUniqueValidator validator.');
    }

    if (!$this->getParameter('columns'))
    {
      throw new sfValidatorException('The "columns" parameter is mandatory for the sfPropelUniqueValidator validator.');
    }

    if (!$this->getParameter('form_primary_keys'))
    {
      throw new sfValidatorException('The "form_primary_keys" parameter is mandatory for the sfPropelUniqueValidator validator.');
    }
    
    if (!$this->getParameter('form_columns'))
    {
      throw new sfValidatorException('The "form_columns" parameter is mandatory for the sfPropelUniqueValidator validator.');
    }

/*
    if (!$this->getParameter('table_name'))
    {
      throw new sfValidatorException('The "table_name" parameter is mandatory for the sfPropelUniqueValidator validator.');
    }
 */   
    return true;
  }
}
